<?php
class MvcPathTest extends PHPUnit_Framework_TestCase {

    const TEST_REQUEST_FULL            = 'Controller/method/param1/param2';
    const TEST_REQUEST_NOT_PARAMS      = 'Controller/method';
    const TEST_REQUEST_ONLY_CONTROLLER = 'Controller';
    const TEST_REQUEST_EMPTY           = '';

    private function _createPathCorrectorMock ($strTestRequest, $controller, $method, $params) {
        $objMvcPathCorrector = $this->getMock(
            'MVC_PathCorrector',
            array('requestCorrect', 'paramCorrect', 'methodCorrect', 'controllerCorrect'),
            array(),
            '',
            false
        );

        $objMvcPathCorrector->expects($this->once())
                            ->method('requestCorrect')
                            ->with($strTestRequest)
                            ->will($this->returnArgument(0));
        $objMvcPathCorrector->expects($this->once())
                            ->method('controllerCorrect')
                            ->with($controller)
                            ->will($this->returnArgument(0));
        $objMvcPathCorrector->expects($this->once())
                            ->method('methodCorrect')
                            ->with($method)
                            ->will($this->returnArgument(0));
        $objMvcPathCorrector->expects($this->once())
                            ->method('paramCorrect')
                            ->with($params)
                            ->will($this->returnArgument(0));

        return $objMvcPathCorrector;
    }


    public function testSetRequest_ParseFullPath () {
        $objMvcPathCorrector = $this->_createPathCorrectorMock(
            self::TEST_REQUEST_FULL,
            'Controller',
            'method',
            array('param1', 'param2')
        );

        $objMvcPath = new MVC_Path();
        $objMvcPath->setMVCPathCorrector($objMvcPathCorrector);
        $objMvcPath->setRequest(self::TEST_REQUEST_FULL);

        $this->assertEquals('Controller', $objMvcPath->getController());
        $this->assertEquals('method', $objMvcPath->getMethod());
        $this->assertEquals(array('param1','param2'), $objMvcPath->getParam());
    }


    public function testSetRequest_ParseRequestNotParams () {
        $objMvcPathCorrector = $this->_createPathCorrectorMock(
            self::TEST_REQUEST_NOT_PARAMS,
            'Controller',
            'method',
            array()
        );

        $objMvcPath = new MVC_Path();
        $objMvcPath->setMVCPathCorrector($objMvcPathCorrector);
        $objMvcPath->setRequest(self::TEST_REQUEST_NOT_PARAMS);

        $this->assertEquals('Controller', $objMvcPath->getController());
        $this->assertEquals('method', $objMvcPath->getMethod());
        $this->assertEquals(array(), $objMvcPath->getParam());
    }


    public function testSetRequest_ParseRequestOnlyConstructor () {
        $objMvcPathCorrector = $this->getMock(
            'MVC_PathCorrector',
            array('requestCorrect', 'paramCorrect', 'methodCorrect', 'controllerCorrect'),
            array(),
            '',
            false
        );

        $objMvcPathCorrector->expects($this->once())
                            ->method('requestCorrect')
                            ->with(self::TEST_REQUEST_ONLY_CONTROLLER)
                            ->will($this->returnValue('Controller/defaultMethod'));
        $objMvcPathCorrector->expects($this->once())
                            ->method('controllerCorrect')
                            ->with('Controller')
                            ->will($this->returnArgument(0));
        $objMvcPathCorrector->expects($this->once())
                            ->method('methodCorrect')
                            ->with('defaultMethod')
                            ->will($this->returnArgument(0));
        $objMvcPathCorrector->expects($this->once())
                            ->method('paramCorrect')
                            ->with(array())
                            ->will($this->returnArgument(0));

        $objMvcPath = new MVC_Path();
        $objMvcPath->setMVCPathCorrector($objMvcPathCorrector);
        $objMvcPath->setRequest(self::TEST_REQUEST_ONLY_CONTROLLER);

        $this->assertEquals('Controller', $objMvcPath->getController());
        $this->assertEquals('defaultMethod', $objMvcPath->getMethod());
        $this->assertEquals(array(), $objMvcPath->getParam());
    }


    public function testSetRequest_ParseRequestEmpty () {
        $objMvcPathCorrector = $this->getMock(
            'MVC_PathCorrector',
            array('requestCorrect', 'paramCorrect', 'methodCorrect', 'controllerCorrect'),
            array(),
            '',
            false
        );

        $objMvcPathCorrector->expects($this->once())
                            ->method('requestCorrect')
                            ->with(self::TEST_REQUEST_ONLY_CONTROLLER)
                            ->will($this->returnValue('DefaultController/defaultMethod'));
        $objMvcPathCorrector->expects($this->once())
                            ->method('controllerCorrect')
                            ->with('DefaultController')
                            ->will($this->returnArgument(0));
        $objMvcPathCorrector->expects($this->once())
                            ->method('methodCorrect')
                            ->with('defaultMethod')
                            ->will($this->returnArgument(0));
        $objMvcPathCorrector->expects($this->once())
                            ->method('paramCorrect')
                            ->with(array())
                            ->will($this->returnArgument(0));

        $objMvcPath = new MVC_Path();
        $objMvcPath->setMVCPathCorrector($objMvcPathCorrector);
        $objMvcPath->setRequest(self::TEST_REQUEST_ONLY_CONTROLLER);

        $this->assertEquals('DefaultController', $objMvcPath->getController());
        $this->assertEquals('defaultMethod', $objMvcPath->getMethod());
        $this->assertEquals(array(), $objMvcPath->getParam());
    }
}